package face.pay.web.device.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import face.pay.constant.UserType;
import face.pay.dao.DeviceAssignmentInfoDao;
import face.pay.dao.DeviceBatchInfoDao;
import face.pay.dao.DeviceOrderInfoDao;
import face.pay.entity.*;
import face.pay.util.ResultMessage;
import face.pay.util.page.PageData;
import face.pay.util.page.PageParam;
import face.pay.web.device.bo.DeviceOrderInfoBO;
import face.pay.web.device.service.DeviceOrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname DeviceOrderInfoServiceImpl
 * @Description JJL
 * @Date 2019/11/11 13:22
 */
@Service
@Slf4j
public class DeviceOrderInfoServiceImpl implements DeviceOrderInfoService {

    @Autowired
    private DeviceOrderInfoDao deviceOrderInfoDao;

    @Autowired
    private DeviceBatchInfoDao deviceBatchInfoDao;

    @Autowired
    private DeviceAssignmentInfoDao deviceAssignmentInfoDao;


    /**
     * 新增导入的信息
     *
     * @param deviceOrderInfoBOList 文件中的数据集合
     * @param deviceBatchInfo 批次类型等
     * @param loginUser 当前登录人
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage addDeviceOrderInfos(UserInfo loginUser, DeviceBatchInfo deviceBatchInfo, List<DeviceOrderInfoBO> deviceOrderInfoBOList) {

        log.info("开始添加数据");
        int size = deviceOrderInfoBOList.size();
        log.info("文件中数据总条数："+ size);

        deviceBatchInfo.setOemId(loginUser.getOemId());
        deviceBatchInfo.setCount(size);
        deviceBatchInfo.setCreatedBy(loginUser.getId()+"");
        deviceBatchInfoDao.insert(deviceBatchInfo); //新增成功返回主键id
        Integer deviceBatchId = deviceBatchInfo.getId(); //主键id

        List<DeviceOrderInfo> deviceOrderInfoList = new ArrayList<>();
        for (DeviceOrderInfoBO list:deviceOrderInfoBOList) {
            DeviceOrderInfo deviceOrderInfo = new DeviceOrderInfo();
            BeanUtils.copyProperties(list,deviceOrderInfo);
            deviceOrderInfoList.add(deviceOrderInfo);
        }

        deviceOrderInfoDao.insert(loginUser.getId(),deviceBatchId,deviceBatchInfo.getDeviceType(),deviceOrderInfoList);
        log.info("生成批次成功,批次id：" + deviceBatchId);

        log.info("结束添加数据");
        return ResultMessage.createSuccess();
    }

    /**
     * 查询当前用户下可使用的设备
     * @param loginUser
     * @param pageParam
     * @return
     */
    @Override
    public PageData queryPage(UserInfo loginUser, PageParam pageParam,DeviceOrderInfo deviceOrderInfo) {

        PageHelper.startPage(pageParam.getPageNo(),pageParam.getPageSize());

        if(UserType.OEM.equals(loginUser.getType()) || UserType.AGENCY.equals(loginUser.getType())){
            deviceOrderInfo.setUserId(loginUser.getId());
        }

        List<DeviceOrderInfo> deviceOrderInfoList = this.deviceOrderInfoDao.getUserDevices(deviceOrderInfo);

        PageInfo<DeviceOrderInfo> pageInfo = new PageInfo <>(deviceOrderInfoList);

        PageData<DeviceOrderInfo> pageData = new PageData<>(pageInfo.getTotal(),deviceOrderInfoList);

        return pageData;
    }

    /**
     * 代理商转出设备
     *
     * @param userId 接收人id
     * @param deviceOrderId 设备订单id
     * @param deviceAssignmentInfo 设备转出信息
     * @return
     */
    @Override
    public ResultMessage assignmentDeviceOrderInfo(UserInfo loginUser,Integer userId, Integer deviceOrderId,
                                                   DeviceAssignmentInfo deviceAssignmentInfo) {

        if (loginUser.getId() == userId) {
            return ResultMessage.createFail("不能将设备转出给自己");
        }
        List<Integer> deviceIdList = new ArrayList<>();
        deviceIdList.add(deviceOrderId);
        deviceOrderInfoDao.update(userId,deviceIdList);
        log.info("代理商："+loginUser.getId()+"将设备sn："+deviceAssignmentInfo.getTerminalSn()+"转出给用户："+userId);

        deviceAssignmentInfo.setGiveUserId(loginUser.getId()); //转出人id
        deviceAssignmentInfo.setCollectUserId(userId); //接收人id
        deviceAssignmentInfo.setUpdatedBy(loginUser.getId()+"");
        deviceAssignmentInfo.setOemId(loginUser.getOemId());
        deviceAssignmentInfoDao.insert(deviceAssignmentInfo);
        log.info("成功生成转出记录");

        return ResultMessage.createSuccess();
    }

    /**
     * admin根据批次查询信息
     *
     * @param batchId
     * @return
     */
    @Override
    public PageData getBatchIdInfo(PageParam pageParam,Integer batchId) {
        PageHelper.startPage(pageParam.getPageNo(),pageParam.getPageSize());

        List<DeviceOrderInfo> deviceOrderInfoList = this.deviceOrderInfoDao.getBatchIdInfo(batchId);

        PageInfo<DeviceOrderInfo> pageInfo = new PageInfo <>(deviceOrderInfoList);

        PageData<DeviceOrderInfo> pageData = new PageData<>(pageInfo.getTotal(),deviceOrderInfoList);

        return pageData;
    }

}
